package com.wlsk.license.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wlsk.license.common.result.Result;
import com.wlsk.license.common.utils.DateUtils;
import com.wlsk.license.dao.entity.Plate;
import com.wlsk.license.dao.entity.PlatePushRecord;
import com.wlsk.license.dao.mapper.PlatePushRecordMapper;
import com.wlsk.license.dto.OpenOrderPlateDto;
import com.wlsk.license.dto.request.OpenOrderPlateInfoReqDto;
import com.wlsk.license.dto.response.OpenOrderPlateResDto;
import com.wlsk.license.service.IFileUploadService;
import com.wlsk.license.service.IOpenApiService;
import com.wlsk.license.service.IPlateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OpenApiServiceImpl implements IOpenApiService {

    @Autowired
    private IPlateService plateService;
    @Autowired
    private PlatePushRecordMapper platePushRecordMapper;
    @Autowired
    private IFileUploadService fileUploadService;

    @Override
    public OpenOrderPlateResDto findOrderPlateInfo(OpenOrderPlateInfoReqDto reqDto) {
        OpenOrderPlateResDto resDto = new OpenOrderPlateResDto();
        List<Plate> plateList = plateService.getBaseMapper().selectList(Wrappers.<Plate>lambdaQuery().in(Plate::getChannelBusinessId, reqDto.getChannelBusinessId()).eq(Plate::getChannelNumber, reqDto.getChannelNumber()).eq(Plate::getDelFlag, 0).orderByDesc(Plate::getId));
        if(CollectionUtils.isNotEmpty(plateList)) {
            for(Plate plate : plateList) {
                OpenOrderPlateDto dto = new OpenOrderPlateDto();
                dto.setChannelBusinessId(plate.getChannelBusinessId());
                dto.setApplyNumber(plate.getApplyNumber());
                dto.setApplyTime(DateUtils.formatLocalDateTime(plate.getCreateTime(), DateUtils.YYYY_MM_DD_HH_MM_SS));
                dto.setPlateNumber(plate.getPlateNumber());
                dto.setCharterPlateNumber(plate.getCharterPlateNumber());
                dto.setTradeAppNumber(plate.getTradeAppNumber());
                dto.setSealStatus(plate.getSealStatus());
                dto.setCharterPlateStatus(plate.getCharterPlateStatus());
                if(StringUtils.isNotEmpty(plate.getLocalFilePath())) {
                    try {
                        dto.setCharterPlateUrl(fileUploadService.getFile(Long.valueOf(plate.getLocalFilePath())));
                    } catch (Exception e) {
                        log.error("获取包车牌图片失败", e);
                    }
                }
                resDto.getApplyList().add(dto);
            }
        }
        return resDto;
    }

    @Override
    @Async
    public Result<Void> pushOrderPlateInfo(Long plateId) {
        try {
            log.info("开始推送包车牌信息，plateId:{}", plateId);
            Plate plate = plateService.getBaseMapper().selectById(plateId);
            if(plate == null) {
                return Result.failed("包车牌不存在");
            }
            PlatePushRecord record = platePushRecordMapper.selectOne(Wrappers.<PlatePushRecord>lambdaQuery().eq(PlatePushRecord::getPlateId, plate.getId()));
            if(record != null) {
                record = new PlatePushRecord();
                record.setPushStatus(2);
                record.setPlateId(plate.getId());
                record.setCharterPlateStatus(plate.getCharterPlateStatus());
                platePushRecordMapper.insert(record);
            } else {
                record.setPushStatus(2);
                record.setCharterPlateStatus(plate.getCharterPlateStatus());
                record.setPushTimes(record.getPushTimes() + 1);
                platePushRecordMapper.updateById(record);
            }
            // TODO 调用第三方推送接口
            record.setPushStatus(1);
            platePushRecordMapper.updateById(record);
        } catch (Exception e) {
            log.error("推送包车牌信息失败", e);
        }
        return Result.success();
    }
}
